﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using LichtspielTests.Tests;

namespace LichtspielTests
{
    public partial class TestLauncher : Form
    {
        List<Type> tests = new List<Type>();

        public TestLauncher() {
            InitializeComponent();

            FillListBox();

            listBox1.SelectedIndex = listBox1.Items.Count-1;

            listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
        }

        void listBox1_DoubleClick(object sender, EventArgs e) {
            StartSelectedTest();
        }

        private void FillListBox() {
            Type[] types = Assembly.GetAssembly(typeof (OpenWindowTest)).GetTypes();            

            foreach (var type in types) {
                if (type.IsSubclassOf(typeof(LichtspielTest)))
                    tests.Add(type);
            }

            foreach (var t in tests) {
                listBox1.Items.Add(t.Name);
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            StartSelectedTest();
        }

        private void StartSelectedTest() {
            string name = ((string)listBox1.SelectedItem);
            Console.WriteLine("*******" + name);

            Type type = null;
            foreach (var t in tests) {
                if (t.Name == name) {
                    type = t;
                }
            }

            if (type != null) {
                Console.WriteLine("******* Starte " + name);

                ConstructorInfo c = type.GetConstructor(new Type[] { });

                if (c != null) {
                    this.Hide();
                    using (LichtspielTest test = (LichtspielTest)c.Invoke(new object[] { })) {
                        test.Run();
                    }
                    this.Show();



                }
            }
        }
    }
}
